/**
 * 
 */
package com.yufei.infoExtractor.core;

import java.io.InputStream;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yufei.infoExtractor.extractor.ContentExtractor;
import com.yufei.infoExtractor.io.HttpDataRetriever;
import com.yufei.infoExtractor.io.DataRetrieverFactory;
import com.yufei.infoExtractor.io.DataRetrieverFeatures;
import com.yufei.infoExtractor.pfw.entity.Relatedlink;
import com.yufei.infoExtractor.util.AppUtil;
import com.yufei.infoExtractor.util.CommonUtil;
import com.yufei.infoExtractor.util.ExceptionUtil;
import com.yufei.infoExtractor.util.FileUtil;
import com.yufei.infoExtractor.util.HtmlUtil;
import com.yufei.infoExtractor.util.sequence.SequenceUUID;

/**
 * @author zhaoyufei zhao-0244@qq.com
 *
 */
public class LinkActionForMedia implements LinkAction {
	private static final Log mLog = LogFactory.getLog(LinkActionForMedia.class);

	/* (non-Javadoc)
	 *just save the media inputstream to the mediaDirectory
	 */
	
	@Override
	public void execute(Relatedlink link, HActionContext context) throws Exception {
		// TODO Auto-generated method stub
		//http://nuomi.xnimg.cn/upload/deal/2012/08/V_L/89811-1.jpg
     String mediaUrl=(String) context.get("mediaUrl");  
     String mediaSaveName=(String) context.get("mediaSaveName");
     String mediaDirectory=AppUtil.getInfoExtractorConfig().getMediaDirectory();
     
     HttpDataRetriever dataRetriever=DataRetrieverFactory.createDataRetriever(new DataRetrieverFeatures(false, null));
     dataRetriever.setUrl(new URL(mediaUrl));
     dataRetriever.connect();
     InputStream input=dataRetriever.getContent();
     FileUtil.makeFile(mediaDirectory, mediaSaveName, input);
     dataRetriever.disconnect();
     
	}
/**
 * @param urls:must be validate url
 * @return  return a collection that hold the pictureName ,if url download failure ,then the pictureName is null
 */
public  List<String>  batchDowloadPcituresFormUrls(List<String> urls){
	List<String> pictureNames=new ArrayList<String>();
    HttpDataRetriever dataRetriever=DataRetrieverFactory.createDataRetriever(new DataRetrieverFeatures(false, null));
    String pictureName=null;
    String mediaDirectory=AppUtil.getInfoExtractorConfig().getMediaDirectory();

    for(String picturerl:urls){
    	/*pictureName = SequenceUUID.getInstance().getUUID()
				+ picturerl.substring(picturerl.lastIndexOf("."));*/
    	//这里生成的图片名称和原始网页中的图片名称保持一致
    	//如果url路径中含有引号的话则直接替换
    	picturerl=picturerl.replace("\"", "");    	
    	picturerl=picturerl.replace("\'", "");

        
    	pictureName=picturerl.substring(picturerl.lastIndexOf("/"));
    	   try {
			dataRetriever.setUrl(new URL(picturerl));
		
    	     dataRetriever.connect();
    	     InputStream input=dataRetriever.getContent();
    	     FileUtil.makeFile(mediaDirectory, pictureName, input);
    	     dataRetriever.disconnect();
    	   } catch (Exception e) {
   			// TODO Auto-generated catch block
   			   mLog.info("wrong when download the image file from url:"+picturerl);
   			   mLog.debug(ExceptionUtil.getExceptionDetailsMessage(e));
   			pictureName=null;
   		}
    }
 return pictureNames;
}

}
